package controller

import (
	"github.com/gin-gonic/gin"
	"mallgo/core"
	"mallgo/internal/common/response"
	"mallgo/internal/resources/user/api/domain/request"
	"mallgo/internal/resources/user/resource"
)

func InitRouter(res *resource.UserResource, s *core.AppServer) {
	// 用户路由
	newUserController := NewUserController(res)
	s.Engine.POST("/register", newUserController.Register)

}

type UserController struct {
	res *resource.UserResource
}

func NewUserController(res *resource.UserResource) *UserController {
	return &UserController{
		res: res,
	}
}

func (c *UserController) Register(ctx *gin.Context) {

	var req request.RegisterRequest

	// 绑定并验证请求数据
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(200, response.Error(400, "请求参数错误"))
		return
	}
	res := c.res.Register(&req)

	ctx.JSON(200, res)
}
